package com.zenithsun.common.review.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.syzg.base.login.model.LoginObject;
import com.syzg.base.merge.model.Person;
import com.syzg.common.config.SysProperties;
import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.common.util.PKCreator;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.SystemUtil;
import com.syzg.solr.SolrManage;
import com.zenithsun.common.bean.ModelType;
import com.zenithsun.common.review.dao.ReviewDAO;
import com.zenithsun.common.review.dao.ReviewdetailDAO;
import com.zenithsun.common.review.model.Review;
import com.zenithsun.common.review.model.ReviewDetailState;
import com.zenithsun.common.review.model.ReviewState;
import com.zenithsun.common.review.model.Reviewdetail;
import com.zenithsun.common.review.service.ReviewdetailService;
import com.zenithsun.news.newsinfo.dao.NewsinfoDAO;
import com.zenithsun.news.newsinfo.model.Newsinfo;

@Service("reviewdetailService")
public class ReviewdetailServiceImpl extends BaseServiceImpl<Reviewdetail> implements ReviewdetailService {
    
    private ReviewdetailDAO reviewdetailDAO = null;
    
    @Autowired
    private ReviewDAO reviewDao=null; 
    
    @Autowired
    private NewsinfoDAO newsinfoDao=null;
    
    @Resource
	public void setreviewDAO(ReviewdetailDAO reviewdetailDAO) {
		this.setDAO(reviewdetailDAO);
		this.reviewdetailDAO = reviewdetailDAO;
	}
    
	@Override
	public boolean createReviewDetail(Reviewdetail reviewdetail) throws Exception {
		try{
			reviewdetail.setId(PKCreator.getPk());
			reviewdetail.setCreateTime(new Date());
			LoginObject loginObj = SystemUtil.getLoginObject();
			Review review=reviewdetail.getReview();
			review.setComments(reviewdetail.getComments());
			if(reviewdetail.getState()==ReviewDetailState.ReviewFail.toShort()){
				review.setState(ReviewState.ReviewFail.toShort());
			}else if(reviewdetail.getState()==ReviewDetailState.ReviewSuccess.toShort()){
				review.setState(ReviewState.ReviewSuccess.toShort());
			}
			if(loginObj!=null){
				Person person=new Person(loginObj.getPersonId());
				reviewdetail.setUserId(person.getId());
				reviewdetail.setPerson(person);
			}else{
				return false;
			}
			/*采用默认登录的生活可以使用
			Person person=this.reviewdetailDAO.getPersonByPersionId(loginObj.getPersonId());
			if(person!=null){
				reviewdetail.setUserId(person.getId());
				reviewdetail.setPerson(person);
			}else{
				return false;
			}
			*/
			this.reviewdetailDAO.create(reviewdetail);
			this.reviewDao.updateNotBlank(review);
			if(ModelType.News.toString().equals(review.getBusMark())&&ReviewState.ReviewSuccess.toShort()==review.getState()){
				Newsinfo newinfo=new Newsinfo();
				newinfo.setId(review.getBusId());
				//发布时间
				newinfo.setPublishTime(new Date());
				//修改时间
				newinfo.setUpdateTime(new Date());
				this.newsinfoDao.publishNews(newinfo);
			}
			//修改更新时间
			this.newsinfoDao.updateNewsinfoUpdateTimeSolr(review.getBusId());
			//solr增量处理
			if("NEWS".equals(SysProperties.SYSTEM_TYPE))
			{
				SolrManage.dataIncrementImport();
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Reviewdetail getCurrReview(String revId) throws Exception {
		SearchMap searchMap = SearchMap.forClass(Reviewdetail.class);
		searchMap.createAlias("a.review", "review");
		searchMap.createAlias("a.person", "person");
		searchMap.eq("a.review.id", revId);
		searchMap.addOrder("a.createTime", SearchMap.ORDER_DESC);
		List<Reviewdetail> list=reviewdetailDAO.findObjects(searchMap);
		Reviewdetail detial=null;
		if(list!=null&&list.size()>0){
			detial=list.get(0);
		}
		return detial;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reviewdetail> listCurrReview(String revId) throws Exception {
		SearchMap searchMap = SearchMap.forClass(Reviewdetail.class);
		//如果去掉以后，则会触发懒加载，会影响效率
		//searchMap.createAlias("a.review", "review");
		//如果去掉以后，则会触发懒加载，会影响效率
		searchMap.createAlias("a.person", "person");
		searchMap.eq("a.review.id", revId);
		searchMap.addOrder("a.createTime", SearchMap.ORDER_DESC);
		List<Reviewdetail> list=reviewdetailDAO.findObjects(searchMap);
		//List<Reviewdetail> list=reviewdetailDAO.listReviewdetail(revId);
		return list;
	}
}